﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace BestPrice.Library
{
    public class WebHandler
    {
        public WebHandler(String baseUrl, String method)
        {
            this.BaseUrl = baseUrl;
            this.Method = method;
        }

        public String BaseUrl { get; set; }
        public String Method { get; set; }

        private HttpClient _HttpClient;
        private HttpClient HttpClient
        {
            get
            {
                if (_HttpClient == null)
                {
                    _HttpClient = new HttpClient();
                    _HttpClient.BaseAddress = new Uri(BaseUrl);
                }
                return _HttpClient;
            }
        }

        public async Task<String> LoadContentAsync(String url, String param, String data, String extraPara)
        {
            if (Method == FormMethod.GET)
            {
                var message = await GetAsync(url, param, data, extraPara);
                return await message.Content.ReadAsStringAsync();
            }
            else
            {
                var message = await PostAsync(url, param, data, extraPara);
                return await message.Content.ReadAsStringAsync();
            }
        }

        public async Task<String> GetContentAsync(String url, String param, String data, String extraPara)
        {
            var message = await GetAsync(url, param, data, extraPara);
            return await message.Content.ReadAsStringAsync();
        }

        public async Task<String> PostContentAsync(String url, String param, String data, String extraPara)
        {
            var message = await PostAsync(url, param, data, extraPara);
            return await message.Content.ReadAsStringAsync();
        }

        public async Task<HttpResponseMessage> GetAsync(String url, String param, String data, String extraPara)
        {
            StringBuilder sb = null;
            var keys = data.Split(' ');
            if (keys.Length > 0)
            {
                sb = new StringBuilder(keys[0].Trim());
                for (int i = 1; i < keys.Length; i++)
                    sb.Append("+" + keys[i].Trim());
                sb.Insert(0, url + "?" + param + "=");
            }
            else
                sb = new StringBuilder(url);

            if (!String.IsNullOrEmpty(extraPara))
            {
                var extras = extraPara.Split(';');
                foreach (var extra in extras)
                {
                    var pas = extra.Split('=');
                    if (pas.Length >= 2)
                        sb.Append('&' + pas[0] + pas[1]);
                }
            }

            return await HttpClient.GetAsync(sb.ToString());
        }

        public async Task<HttpResponseMessage> PostAsync(String url, String param, String data, String extraPara)
        {
            data = data.Replace("  ", " ");
            data = data.Replace(' ', '+');
            
            if (!String.IsNullOrEmpty(extraPara))
            {
                var extras = extraPara.Split(';');
                foreach (var extra in extras)
                {
                    var pas = extra.Split('=');
                    if (pas.Length >= 2)
                        data += ('&' + pas[0] + '=' + pas[1]);
                }
            }

            var dicData = new Dictionary<String, String>();
            dicData.Add(param, data);
            var content = new FormUrlEncodedContent(dicData);

            return await HttpClient.PostAsync(url, content);
        }
    }

    public struct FormMethod
    {
        public const String GET = "GET";
        public const String POST = "POST";
        public const String PUT = "PUT";
    }
}
